package com.example.pingtools

import android.R.attr.data
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.mygreendao.GreenDaoManager
import com.example.pingtools.adpater.IpItemAd
import com.example.pingtools.adpater.MenuAd
import com.example.pingtools.base.confirmdialog
import com.example.pingtools.base.dialog
import com.example.pingtools.base.dialogfloder
import com.example.pingtools.bean.FloderBean
import com.example.pingtools.bean.IpDataBean
import com.example.pingtools.bean.isupdate
import com.example.pingtools.util.ShareUtil
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class MainActivity : AppCompatActivity(),View.OnClickListener{
    var menad:MenuAd?=null
    var ipad:IpItemAd?=null
    var floderid:Long=0
    var flodername:String?="无"
    var list:MutableList<FloderBean>?=null
    var iplist:MutableList<IpDataBean>?= arrayListOf()
    var ipnum:Int=0;
    var spfloderid: String?=null
    var isupdatefloder=false
    var flag:Int=0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT)
        {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        }
        EventBus.getDefault().register(this)
        setContentView(R.layout.activity_main)
        init()
        initmenuAd()

        if(list!!.size!=0) {
            spfloderid= ShareUtil.getString("floderid", this)
            var floder_name=ShareUtil.getString("flodername", this)
            if(spfloderid==null||spfloderid==""||spfloderid=="-1"){
                floderid = list!!.get(0).id
            }else {
                Log.d("xxxxxxxxxxxx",spfloderid+"")
                floderid =spfloderid!!.toLong()
                flodername=floder_name

            }
        }
        initipAd()
        Log.d("sssssss","sdsda")
        refreshLayout.setOnRefreshListener(OnRefreshListener {
            val message = Message()
            message.what = 1
            message.obj = data
            handler.sendMessageDelayed(message, 1000)
        })
        ppbb.setText("V"+this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName)

    }


   public fun initmenuAd(){
        list=GreenDaoManager(this).findFloderall()
       if(list!!.size!=0) {
           flodername=list!!.get(0).name
           menad = MenuAd(list!!)
           rl_menu.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
           rl_menu.adapter = menad
           menad!!.setOnKotlinItemClickListener(object : MenuAd.IKotlinItemClickListener {
               override fun onItemClickListener(position: Int) {
                   floderid=list!!.get(position).id
                   //initipAd()
                   flodername=list!!.get(position).name;
                   refreshLayout.autoRefresh()
                   //tv_floder_name.text=list!!.get(position).name+"("+ipnum+")"
                   ShareUtil.putString("floderid", floderid.toString(), this@MainActivity)
                   ShareUtil.putString("flodername", flodername, this@MainActivity)

               }

               override fun onItemClickListenerdel(position: Int) {


                       if(list!!.size!=0) {
                           val  dialog1 = confirmdialog(this@MainActivity,iplist,list,position,2)
                           dialog1.show()
                      /* floderid = list!!.get(0).id
                       initipAd()*/
                       }

               }
           })
           if(isupdatefloder==true){
               flodername=list!!.get(list!!.size-1).name
               floderid= list!!.get(list!!.size-1).id
               //initipAd()
               refreshLayout.autoRefresh()
           }
       }

    }



    fun initipAd() {
        var FloderBeans = GreenDaoManager(this).findFloder(floderid.toString())
        if (FloderBeans.size != 0) {
            var datalist = FloderBeans!!.get(0).ipDataBeans
            iplist!!.clear()
            iplist!!.addAll(datalist)
            ipnum=iplist!!.size;
            ipad = IpItemAd(this, iplist!!)
            tv_floder_name.text =flodername+"("+ipnum+")"
            if(flag==0){
                flag++
                iprl()

            }else{
                ipad = IpItemAd(this, iplist!!)
                rl_ip.adapter = ipad
                Log.d("wwwwwwwwww","qweqwe")
                ipadclick()
                ipad!!.notifyDataSetChanged()
            }
            refreshLayout.finishRefresh(true);

        }
    }

    fun iprl(){
        val linearLayoutManager: LinearLayoutManager =
            object : LinearLayoutManager(this, VERTICAL, false) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }

//解决数据加载不完的问题
        /*   rl_ip.isNestedScrollingEnabled = false
           rl_ip.setHasFixedSize(true)
//解决数据加载完成后, 没有停留在顶部的问题
           rl_ip.isFocusable = false*/
        rl_ip.layoutManager =linearLayoutManager //LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rl_ip.adapter = ipad
        ipadclick()
    }

    fun ipadclick(){
        ipad!!.setOnKotlinItemClickListener(object : IpItemAd.IKotlinItemClickListener {
            override fun iponItemClickListener(position: Int) {
                val  dialog1 = dialog(this@MainActivity,floderid,position,iplist)
                dialog1.show()
            }

            override fun iponItemClickListenerdel(position: Int) {
                val  dialog1 = confirmdialog(this@MainActivity,iplist,list,position,1)
                dialog1.show()
                //initipAd()
            }


        })
       // refreshLayout.finishRefresh(true);
    }

    fun init(){
        add_ip!!.setOnClickListener(this)
        tv_update!!.setOnClickListener(this)
        add_floder!!.setOnClickListener(this)
        tv_updateip!!.setOnClickListener(this)
    }



    override fun onClick(v: View?) {
        when(v?.id){
            R.id.add_ip ->{
                if(floderid.toString()=="0"){
                    Toast.makeText(this,"请先选择群组",Toast.LENGTH_SHORT).show()
                }else{
                    val  dialog1 = dialog(this,floderid,0,null)
                    dialog1.show()
                }

            }
            R.id.tv_update ->{
                Toast.makeText(this,"设备刷新成功",Toast.LENGTH_SHORT).show()

                initmenuAd()
                // initipAd()
            }
            R.id.add_floder ->{
                val  dialog1 = dialogfloder(this)
                dialog1.show()
             //   initmenuAd()
            }
            R.id.tv_updateip ->{
                Toast.makeText(this,"ip刷新成功",Toast.LENGTH_SHORT).show()
                refreshLayout.autoRefresh()
               // initipAd()

            }

        }
    }

    private val handler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                1 -> {
                    initipAd()
                    //refreshLayout.autoRefresh()
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun subScribeFun2(isupdate: isupdate){
       if(isupdate.isud==1){

           if(isupdate.isdel) {
               isupdatefloder=false
               initmenuAd()
               if(spfloderid==isupdate.id.toString()){
                   Log.d("xxxxxxxxxxxx",spfloderid+"---"+isupdate.id)
                   ShareUtil.putString("floderid", "-1", this)
                   ShareUtil.putString("flodername", "-1", this)
               }
               if(list!!.size!=0) {
                   floderid = list!!.get(0).id
                   //initipAd()
                   refreshLayout.autoRefresh()
               }else{
                   floderid=0
                   flodername="无"
                    ipnum=0;
                   iplist!!.clear()
                   ipad = IpItemAd(this@MainActivity, iplist!!)
                   //rl_ip.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false)
                   rl_ip.adapter = ipad
                   ipadclick()
                   ipad!!.notifyDataSetChanged()


                   list!!.clear()
                   menad = MenuAd(list!!)
                   rl_menu.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.HORIZONTAL, false)
                   rl_menu.adapter = menad
                 //  tv_floder_name.text = flodername
               }
               tv_floder_name.text = flodername+"("+ipnum+")"
           }else{
               //tv_floder_name.text = flodername+"("+ipnum+")"
              // Toast.makeText(this,"设备刷新成功"+isupdate.id,Toast.LENGTH_SHORT).show()
               isupdatefloder=true
               Log.d("ffffffffffff","adasda")
               initmenuAd()
           }

       }else{
           //initipAd()
           refreshLayout.autoRefresh()
           //tv_floder_name.text = flodername+"("+ipnum+")"
         //  ipad!!.notifyDataSetChanged()
           Log.d("ffffffffffff","wqewe")
       }

    }


}






